package com.qidian.mes.pro.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.pojo.Proces;
import com.qidian.mes.pojo.WorkTicket;
import com.qidian.mes.pro.mapper.ProcessMapper;
import com.qidian.mes.pro.service.ProcesService;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author chenzheng
 * @Company 起点编程
 * @Time 2023/10/27 19:23
 * @Description
 */
@Service
public class ProcesServiceImpl implements ProcesService {
    @Autowired
    private ProcessMapper processMapper;
    @Override
    public MESResult queryById(Long processId) {
        Proces proces = null;
        if ((proces = processMapper.selectById(processId)) != null){
            //System.out.println(proces);
            return MESResult.ok(proces);
        }
        return MESResult.empty();
    }

    @Override
    public MESResult save(Proces proces) {
        // 校验用户名是否重复
        if(proces.getProcessName() != null && processMapper.selectByProcessName(proces.getProcessName()) != null){
            return MESResult.duplicate("用户名重复");
        }
        // updateIsOk会根据传入的参数判断返回的结果
        return MESResult.updateIsOk(processMapper.save(proces));
    }

    @Override
    public MESResult update(Proces proces) {
        // 校验是否重复
        Proces proces1 = processMapper.selectByProcessName(proces.getProcessName());
        if(proces1 != null && !proces1.getProcessId().equals(proces.getProcessId())){
            return MESResult.duplicate("用户名重复");
        }
        return MESResult.updateIsOk(processMapper.update(proces));
    }

    @Override
    public MESResult delete(Long processId) {
        return MESResult.updateIsOk(processMapper.delete(processId));
    }

    @Override
    public MESResult list(int page, int limit, Proces proces) {
// 开启分页 在当前线程中执行下一条SQL的时候就会进行分页。
        PageHelper.startPage(page,limit);
        List<Proces> list = processMapper.list(proces);
// 组织一个分页对象。（页码，每页大小，最大页码，数据，总条数）
        PageInfo<Proces> pageInfo = new PageInfo<>(list);
        return MESResult.ok(pageInfo);
    }
}
